﻿using DBUtility;
using Jmit.Common;
using Jmit.Deyi.AutoTask;
using Jmit.Deyi.AutoTask.DataSync;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

namespace Jmit.Deyi.Job
{
    // 不允许此 Job 并发执行任务（禁止新开线程执行）
    // [DisallowConcurrentExecution]

    // 商数通-同步进销数据-DSHJob
    public class DSHJob : IJob
    {

        /// <summary>
        /// 定时任务：从orcale取数据同步到MySQL
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Factory.StartNew(() =>
            {

                LogBLL.log("版本："+ AppManage.appVersion);
                //-------每日数据同步

                // 新账号是否同步
                string asyn_time = DateTime.Now.ToString("yyyy-MM-dd"); // 当日

                string new_fl = "";
                // 判断是否新账号第一次同步数据
                if (Form1.g_newfromname !="")
                {
                    //new_fl = AppManage.newFromName;
                    new_fl = Form1.g_newfromname;
                }


                // 同步数据
                // 3个视图是用来统计流向的，从批发出数据
                sync_DSHJH(new_fl, asyn_time);       // 进货
                sync_DSNXS(new_fl, asyn_time);       // 销售（导出用）
                sync_DSNKC(new_fl, asyn_time);       // 库存

                // 两个视图是统计门店实际销售情况的
                sync_DSH_SELL(new_fl, asyn_time);    // 销售（分析用）
                sync_DSNFXCS(new_fl, asyn_time);     // 数据分析 // 2024-01-03 add


                // 20251014 add 复核分类、陈列分类、商品档案
                sync_DSNSUPPLY();      // 复合分类 
                sync_DSNCLFL();        // 陈列分类 
                sync_TBV_ARTICLE();    // 商品档案 


                //-------手动同步数据


                /* DateTime startDate = new DateTime(2024, 3, 1);
                 DateTime currentDate = DateTime.Now;

                 while (startDate <= currentDate)
                 {
                     string formattedDate = startDate.ToString("yyyy-MM-dd");


                     sync_DSH_SELL(formattedDate);    // 在这里执行您的代码，使用 formattedDate 变量作为日期

                     startDate = startDate.AddDays(1); // 增加一天
                 }
 */
                //----------
                //sync_DSNFXFHFL(); // 档案表-复合分类（同步一次）
                //sync_DSNSKLX(); // 档案表-收款类型（同步一次）




                Form1.g_newfromname = "";

            });
        }

        /// <summary>
        /// 进货
        /// </summary>
        private void sync_DSHJH(string new_from_name, string asyn_time)
        {

            string table_name = "DSNJH";
            /*if (from_id == "0") { table_name = "DSNJH"; }
            if (from_id == "1") { table_name = "湖南沐阳进货"; }
            if (from_id == "2") { table_name = "爱诺进货"; }
            if (from_id == "3") { table_name = "艾条进货"; }*/

            string sync_name = "[数据表："+ table_name + "]";
            string sync_message = "";
            string sync_result = "fail";
            int sync_count = 0;
            string sale_count = "";

            string strCnn_from = AppConfigManager.LoadConfig().dbConn_from;
            string strCnn_to = AppConfigManager.LoadConfig().dbConn_to;



            try
            {
                //Console.WriteLine(@" Hello Quartz.Net");
                LogBLL.log("---" + sync_name + "--- 开始");
                





                //查询主档数据
                //string asyn_time = DateTime.Now.ToString("yyyy-MM-dd");

                string sql_from = "";
                if(new_from_name == "")
                {
                    sql_from = $" select * from " + table_name + " where 日期='" + asyn_time + "'"; // 同步每日数据

                }
                else
                {
                    sql_from = $" select * from "+ table_name + " where 1=1 and 分类 = '"+ new_from_name + "' order by 日期 ";    // 新账号 第一次同步数据
                }

                LogBLL.log("sql_from：" + sql_from);

                DataSet ds = OracleHelper.Query(strCnn_from, sql_from);
                DataTable DT = ds.Tables[0];
                if (DT != null && DT.Rows.Count > 0)
                {
                    LogBLL.log("行数：" + DT.Rows.Count);

                    List<string> sqlList = new List<string>();


                    foreach (DataRow row in DT.Rows)
                    {

                        /*string deptid = row["deptid"].ToString();
                        string deptno = row["deptno"].ToString();
                        string deptname = row["deptname"].ToString();*/

                        string ymd = String2Json(row["日期"].ToString());
                        string seller_code = String2Json(row["销售方代码"].ToString());
                        string seller_name = String2Json(row["销售方名称"].ToString());
                        string purchaser_code = String2Json(row["采购方代码"].ToString());
                        string purchaser_name = String2Json(row["采购方名称"].ToString());
                        string prd_code = String2Json(row["产品代码"].ToString());
                        string prd_name = String2Json(row["产品名称"].ToString());
                        string prd_spec = String2Json(row["产品规格"].ToString());

                        

                        string prd_batch = String2Json(row["批号"].ToString());
                        string prd_qty = String2Json(row["数量"].ToString());
                        string prd_unit = String2Json(row["单位"].ToString());
                        string prd_price = String2Json(row["单价"].ToString());
                        string total = String2Json(row["金额"].ToString());
                        string mfrs = String2Json(row["生产厂家"].ToString());
                        string pzwh = String2Json(row["批准文号"].ToString());

                        // 新增其他厂商-数据来源
                        string from_id = "0";
                        string from_name = String2Json(row["分类"].ToString());
                        from_id = AppManage.GetFromDict(from_name);
                        /*if (from_name == "8001N4 湖南沐阳") { from_id = "1"; }
                        if (from_name == "8001y6 爱诺") { from_id = "2"; }
                        if (from_name == "8001D3 艾条") { from_id = "3"; }
                        if (from_name == "8001B2 雷允上销售分公司") { from_id = "4"; }
                        if (from_name == "8001E6 初贝") { from_id = "5"; }
                        if (from_name == "8001B5 丽珠集团") { from_id = "5"; }
                        if (from_name == "8001P3 安徽安科") { from_id = "6"; }
                        if (from_name == "8001D1 贵州德良方药业") { from_id = "7"; }*/



                        string sql = $" insert INTO `dsnjh`(" +
                            $"`ymd`," +
                            $" seller_code," +
                            $" seller_name," +
                            $" purchaser_code ," +
                            $" purchaser_name ," +
                            $" prd_code ," +
                            $" prd_name ," +
                            $" prd_spec ," +
                            $" prd_batch ," +
                            $" prd_qty ," +
                            $" prd_unit ," +
                            $" prd_price ," +
                            $" total ," +
                            $" mfrs ," +
                            $" pzwh ," +
                            $" from_id ," +
                            $" from_name ," +
                            $" made_time " +
                            $") " +
                            $"VALUES (" +
                            $"'{ymd}'," +
                            $" '{seller_code}'," +
                            $" '{seller_name}'," +
                            $"'{purchaser_code}'," +
                            $"'{purchaser_name}'," +
                            $"'{prd_code}'," +
                            $"'{prd_name}'," +
                            $"'{prd_spec}'," +
                            $"'{prd_batch}'," +
                            $"'{prd_qty}'," +
                            $"'{prd_unit}'," +
                            $"'{prd_price}'," +
                            $"'{total}'," +
                            $"'{mfrs}'," +
                            $"'{pzwh}'," +
                            $"'{from_id}'," +
                            $"'{from_name}'," +
                            $"now()" +
                            $") ";


                        sqlList.Add(sql);
                        //LogBLL.log("sql：" + sql);
                    }

                    //sqlList.Add("call test_log()");

                    //LogBLL.log("SQL执行开始");
                    //sqlList.Add("call editGoodsKey()");

                    bool isok = MyOpDB.SaveDataToDB(strCnn_to, sqlList);
                    LogBLL.log("SQL执行:" + isok);


                    //LogBLL.log("SQL执行结束:" + isok);

                    sync_count = DT.Rows.Count;
                    sync_message = "同步数据成功，数量："+ sync_count;
                    sync_result = "success";
                }
                else
                {
                    LogBLL.log("没有数据");

                    sync_count = 0;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
            }
            catch (Exception ex)
            {
                sync_message = "同步数据失败，错误消息=" + ex.Message;
                sync_result = "error";
                LogBLL.Err(sync_message);
            }
            finally
            {
                try
                {
                    // 记录日志
                    MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_sync_log (sync_message,sync_result,sync_count,made_time) VALUES ('{sync_name} {sync_message}', '{sync_result}', '{sync_count}',now()) ");

                }
                catch (Exception e)
                {

                }
                // 统计当天销售数据
                // MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_zdxx_log (sync_message,sync_result,sync_count,made_time) VALUES ('今日销售数据数量：{sale_count}条', '{sync_result}', '{sale_count}',now()) ");

            }
        }

        /// <summary>
        /// 销售（分析用）
        /// </summary>
        private void sync_DSH_SELL(string new_from_name, string asyn_time)
        {

            string table_name = "DSNCESHI";
            /*if (from_id == "0") { table_name = "DSHKC"; }
            if (from_id == "1") { table_name = "湖南沐阳库存"; }
            if (from_id == "2") { table_name = "爱诺库存"; }
            if (from_id == "3") { table_name = "艾条库存"; }*/

            string sync_name = "[数据表：" + table_name + "]";
            string sync_message = "";
            string sync_result = "fail";
            int sync_count = 0;
            string sale_count = "";

            string strCnn_from = AppConfigManager.LoadConfig().dbConn_from;
            string strCnn_to = AppConfigManager.LoadConfig().dbConn_to;

            try
            {
                LogBLL.log("---" + sync_name + "--- 开始");




                //查询主档数据
                //string asyn_time = DateTime.Now.ToString("yyyy-MM-dd"); // 同步日期
                string sql_from = "";
                if (new_from_name == "")
                {
                    sql_from = "select * from DSNCESHI where  to_char( 销售日期,'yyyy-mm-dd') ='" + asyn_time + "'"; // 同步每日数据

                }
                else
                {
                    sql_from = $" select * from DSNCESHI where 1=1 and 分类编码 = '"+ new_from_name + "'  order by 销售日期  "; // 新账号 第一次同步数据

                }




                DataSet ds = OracleHelper.Query(strCnn_from, sql_from);
                DataTable DT = ds.Tables[0];
                if (DT != null && DT.Rows.Count > 0)
                {
                    LogBLL.log("行数：" + DT.Rows.Count);

                    List<string> sqlList = new List<string>();


                    foreach (DataRow row in DT.Rows)
                    {

                        string ymd              = String2Json(row["销售日期"].ToString());  

                        string articode         = String2Json(row["操作码"].ToString());// tbb_article.articode 操作码
                        string class_id         = String2Json(row["商品ID"].ToString());
                        string prd_code         = String2Json(row["商品ID"].ToString());
                        string prd_name         = String2Json(row["品名"].ToString());
                        string prd_spec         = String2Json(row["规格"].ToString());
                        string mfrs             = String2Json(row["生产厂商"].ToString());
                        string prd_unit         = String2Json(row["基本单位"].ToString());
                        string tax_rate         = String2Json(row["商品税率"].ToString());
                        string retail_price     = String2Json(row["商品零售价"].ToString());
                        string sell_qty         = String2Json(row["商品销售数量"].ToString());
                        string sell_price       = String2Json(row["商品销售价"].ToString());
                        string sell_amt         = String2Json(row["销售金额"].ToString());
                        string dept_code        = String2Json(row["销售部门编码"].ToString());
                        string dept_name        = String2Json(row["销售部门名称"].ToString());
                        string dept_addr        = String2Json(row["销售部门地址"].ToString());
                        string order_id         = String2Json(row["销售总单号"].ToString());
                        string order_line_id    = String2Json(row["销售细单号"].ToString());
                        string prd_batch        = String2Json(row["商品批号"].ToString());
                        string period           = String2Json(row["有效期"].ToString());
                        string pzwh             = String2Json(row["商品批准文号"].ToString());
                        string seller_code      = String2Json(row["销售人员编码"].ToString());
                        string seller_name      = String2Json(row["销售人员姓名"].ToString());

                        // 新增其他厂商-数据来源
                        string from_id = "0";
                        string from_name = String2Json(row["分类编码"].ToString());
                        from_id = AppManage.GetFromDict(from_name);
                        /*if (from_name == "8001N4 湖南沐阳") { from_id = "1"; }
                        if (from_name == "8001y6 爱诺") { from_id = "2"; }
                        if (from_name == "8001D3 艾条") { from_id = "3"; }
                        if (from_name == "8001B2 雷允上销售分公司") { from_id = "4"; }
                        if (from_name == "8001E6 初贝") { from_id = "5"; }
                        if (from_name == "8001B5 丽珠集团") { from_id = "5"; }
                        if (from_name == "8001P3 安徽安科") { from_id = "6"; }
                        if (from_name == "8001D1 贵州德良方药业") { from_id = "7"; }*/


                        string sql = $" insert INTO `dsn_sell`(" +
                            $"`ymd`," +
                            $" articode ," +
                            $" class_id ," +
                            $" prd_code ," +
                            $" prd_name ," +
                            $" prd_spec ," +
                            $" mfrs ," +
                            $" prd_unit ," +
                            $" tax_rate ," +
                            $" retail_price ," +
                            $" sell_qty ," +
                            $" sell_price  ," +
                            $" sell_amt ," +
                            $" dept_code ," +
                            $" dept_name ," +
                            $" dept_addr ," +
                            $" order_id ," +
                            $" order_line_id ," +
                            $" prd_batch ," +
                            $" period ," +
                            $" pzwh ," +
                            $" from_id ," +
                            $" from_name ," +
                            $" seller_code ," +
                            $" seller_name ," +
                            $" made_time " +
                            $") " +
                            $"VALUES (" +
                            $"'{ymd}'," +
                            $"'{articode}'," +
                            $"'{class_id}'," +
                            $"'{prd_code}'," +
                            $"'{prd_name}'," +
                            $"'{prd_spec}'," +
                            $"'{mfrs}'," +
                            $"'{prd_unit}'," +
                            $"'{tax_rate}'," +
                            $"'{retail_price}'," +
                            $"'{sell_qty}'," +
                            $"'{sell_price}'," +
                            $"'{sell_amt}'," +
                            $"'{dept_code}'," +
                            $"'{dept_name}' ," +
                            $"'{dept_addr}'," +
                            $"'{order_id}'," +
                            $"'{order_line_id}'," +
                            $"'{prd_batch}'," +
                            $"'{period}'," +
                            $"'{pzwh}'," +
                            $"'{from_id}'," +
                            $"'{from_name}'," +
                            $"'{seller_code}'," +
                            $"'{seller_name}'," +
                            $"now()" +
                            $") ";


                        sqlList.Add(sql);
                    }



                    bool isok = MyOpDB.SaveDataToDB(strCnn_to, sqlList);
                    LogBLL.log("SQL执行:" + isok);


                    //LogBLL.log("SQL执行结束:" + isok);

                    sync_count = DT.Rows.Count;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
                else
                {
                    LogBLL.log("没有数据");
                    sync_count = 0;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
            }
            catch (Exception ex)
            {
                sync_message = "同步数据失败，错误消息=" + ex.Message;
                sync_result = "error";
                LogBLL.Err(sync_message);
            }
            finally
            {
                try
                {
                    // 记录日志
                    MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_sync_log (sync_message,sync_result,sync_count,made_time) VALUES ('{sync_name} {sync_message}', '{sync_result}', '{sync_count}',now()) ");

                }
                catch (Exception e)
                {

                }
                // 统计当天销售数据
                // MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_zdxx_log (sync_message,sync_result,sync_count,made_time) VALUES ('今日销售数据数量：{sale_count}条', '{sync_result}', '{sale_count}',now()) ");

            }
        }

        /// <summary>
        /// 销售（导出用）
        /// </summary>
        private void sync_DSNKC(string new_from_name, string asyn_time)
        {

            string table_name = "DSNKC";
            /*if (from_id == "0") { table_name = "DSHKC"; }
            if (from_id == "1") { table_name = "湖南沐阳库存"; }
            if (from_id == "2") { table_name = "爱诺库存"; }
            if (from_id == "3") { table_name = "艾条库存"; }*/

            string sync_name = "[数据表：" + table_name + "]";

            string sync_message = "";
            string sync_result = "fail";
            int sync_count = 0;
            string sale_count = "";

            string strCnn_from = AppConfigManager.LoadConfig().dbConn_from;
            string strCnn_to = AppConfigManager.LoadConfig().dbConn_to;

            try
            {
                LogBLL.log("---" + sync_name + "--- 开始");




                //查询主档数据
                //string asyn_time = DateTime.Now.ToString("yyyy-MM-dd");

                string sql_from = "";
                if (new_from_name == "")
                {
                    sql_from = $" select * from " + table_name + " where 日期='" + asyn_time + "'  ";   // 同步每日数据
                }
                else
                {
                    sql_from = $" select * from " + table_name + " where 1=1 and 分类 ='" + new_from_name + "' order by 日期 ";       // 新账号 第一次同步数据
                }




                DataSet ds = OracleHelper.Query(strCnn_from, sql_from);
                DataTable DT = ds.Tables[0];
                if (DT != null && DT.Rows.Count > 0)
                {
                    LogBLL.log("行数：" + DT.Rows.Count);

                    List<string> sqlList = new List<string>();


                    foreach (DataRow row in DT.Rows)
                    {

                        /*string deptid = row["deptid"].ToString();
                        string deptno = row["deptno"].ToString();
                        string deptname = row["deptname"].ToString();*/

                        string ymd = String2Json(row["日期"].ToString());
                        string comp_code = String2Json(row["公司代码"].ToString());
                        string comp_name = String2Json(row["公司名称"].ToString());
                        string prd_code = String2Json(row["产品代码"].ToString());
                        string prd_name = String2Json(row["产品名称"].ToString());
                        string prd_spec = String2Json(row["产品规格"].ToString());
                        string prd_batch = String2Json(row["批号"].ToString());
                        string prd_qty = String2Json(row["数量"].ToString());
                        string prd_unit = String2Json(row["单位"].ToString());
                        //string prd_price = "0";
                        string prd_price = String2Json(row["单价"].ToString());
                        string period = String2Json(row["有效期"].ToString());
                        //string inv_status = "正常";
                        string inv_status = String2Json(row["库存状态"].ToString());
                        string mfrs = String2Json(row["生产厂家"].ToString());
                        string pzwh = String2Json(row["批准文号"].ToString());

                        // 新增其他厂商-数据来源
                        string from_id = "0";
                        string from_name = String2Json(row["分类"].ToString());
                        from_id = AppManage.GetFromDict(from_name);
                        /*if (from_name == "8001N4 湖南沐阳") { from_id = "1"; }
                        if (from_name == "8001y6 爱诺") { from_id = "2"; }
                        if (from_name == "8001D3 艾条") { from_id = "3"; }
                        if (from_name == "8001B2 雷允上销售分公司") { from_id = "4"; }
                        if (from_name == "8001E6 初贝") { from_id = "5"; }
                        if (from_name == "8001B5 丽珠集团") { from_id = "5"; }
                        if (from_name == "8001P3 安徽安科") { from_id = "6"; }
                        if (from_name == "8001D1 贵州德良方药业") { from_id = "7"; }*/


                        string sql = $" insert INTO `dsnkc`(" +
                            $"`ymd`," +
                            $" comp_code ," +
                            $" comp_name ," +
                            $" prd_code ," +
                            $" prd_name ," +
                            $" prd_spec ," +
                            $" prd_batch ," +
                            $" prd_qty ," +
                            $" prd_unit ," +
                            $" prd_price ," +
                            $" period ," +
                            $" inv_status ," +
                            $" mfrs ," +
                            $" pzwh, " +
                            $" from_id, " +
                            $" from_name, " +
                            $" made_time " +
                            $") " +
                            $"VALUES (" +
                            $"'{ymd}'," +
                            $"'{comp_code}'," +
                            $"'{comp_name}'," +
                            $"'{prd_code}'," +
                            $"'{prd_name}'," +
                            $"'{prd_spec}'," +
                            $"'{prd_batch}'," +
                            $"'{prd_qty}'," +
                            $"'{prd_unit}'," +
                            $"'{prd_price}'," +
                            $"'{period}'," +
                            $"'{inv_status}'," +
                            $"'{mfrs}'," +
                            $"'{pzwh}'," +
                            $"'{from_id}'," +
                            $"'{from_name}'," +
                            $"now()" +
                            $") ";


                        sqlList.Add(sql);
                        
                    }

                    //sqlList.Add("call test_log()");

                    //LogBLL.log("SQL执行开始");
                    //sqlList.Add("call editGoodsKey()");
                    bool isok = MyOpDB.SaveDataToDB(strCnn_to, sqlList);
                    LogBLL.log("SQL执行:" + isok);


                    //LogBLL.log("SQL执行结束:"+ isok);

                    sync_count = DT.Rows.Count;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
                else
                {
                    LogBLL.log("没有数据");
                    sync_count = 0;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
            }
            catch (Exception ex)
            {
                sync_message = "同步数据失败，错误消息=" + ex.Message;
                sync_result = "error";
                LogBLL.Err(sync_message);
            }
            finally
            {
                try
                {
                    // 记录日志
                    MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_sync_log (sync_message,sync_result,sync_count,made_time) VALUES ('{sync_name} {sync_message}', '{sync_result}', '{sync_count}',now()) ");

                }
                catch (Exception e)
                {

                }
                // 统计当天销售数据
                // MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_zdxx_log (sync_message,sync_result,sync_count,made_time) VALUES ('今日销售数据数量：{sale_count}条', '{sync_result}', '{sale_count}',now()) ");

            }
        }



        /// <summary>
        /// 销售（发货到门店）
        /// </summary>
        private void sync_DSNXS(string new_from_name, string asyn_time)
        {

            string table_name = "DSNXS";
            /*if (from_id == "0") { table_name = "DSNXS"; }
            if (from_id == "1") { table_name = "湖南沐阳销售"; }
            if (from_id == "2") { table_name = "爱诺销售"; }
            if (from_id == "3") { table_name = "艾条销售"; }*/

            string sync_name = "[数据表：" + table_name + "]";
            string sync_message = "";
            string sync_result = "fail";
            int sync_count = 0;
            string sale_count = "";

            string strCnn_from = AppConfigManager.LoadConfig().dbConn_from;
            string strCnn_to = AppConfigManager.LoadConfig().dbConn_to;

            try
            {
                LogBLL.log("---" + sync_name + "--- 开始");


                //查询主档数据
                //string asyn_time = DateTime.Now.ToString("yyyy-MM-dd");

                string sql_from = "";
                if(new_from_name == "")
                {
                    sql_from = $" select * from " + table_name + " where 日期 = '" + asyn_time + "' order by 日期 ";    // 同步每日数据
                }
                else
                {
                    sql_from = $" select * from " + table_name + " where 1=1 and 分类 = '" + new_from_name + "' order by 日期 ";                       // 新账号 第一次同步数据
                }


                LogBLL.log(sql_from);


                DataSet ds = OracleHelper.Query(strCnn_from, sql_from);
                DataTable DT = ds.Tables[0];
                if (DT != null && DT.Rows.Count > 0)
                {
                    LogBLL.log("行数：" + DT.Rows.Count);

                    List<string> sqlList = new List<string>();


                    foreach (DataRow row in DT.Rows)
                    {

                        /*string deptid = row["deptid"].ToString();
                        string deptno = row["deptno"].ToString();
                        string deptname = row["deptname"].ToString();*/

                        string ymd = String2Json(row["日期"].ToString());
                        string seller_code = String2Json(row["销售方代码"].ToString());
                        string seller_name = String2Json(row["销售方名称"].ToString());
                        string purchaser_code = String2Json(row["采购方代码"].ToString());
                        string purchaser_name = String2Json(row["采购方名称"].ToString());
                        string articode = String2Json(row["产品代码"].ToString());
                        string class_id = String2Json(row["产品id"].ToString());      //  new add    // tbb_article.artiid 产品id
                        string prd_code = String2Json(row["产品id"].ToString());
                        string prd_name = String2Json(row["产品名称"].ToString());
                        string prd_spec = String2Json(row["产品规格"].ToString());
                        string prd_batch = String2Json(row["批号"].ToString());
                        string prd_qty = String2Json(row["数量"].ToString());
                        string prd_unit = String2Json(row["单位"].ToString());
                        string prd_price = String2Json(row["单价"].ToString());
                        string total = String2Json(row["金额"].ToString());
                        string mfrs = String2Json(row["生产厂家"].ToString());
                        string pzwh = String2Json(row["批准文号"].ToString());
                        //string sell_type = String2Json(row["销售类型"].ToString());
                        string sell_type = "销售出库";
                        string delivery_address = String2Json(row["收货地址"].ToString());

                        // 新增其他厂商-数据来源
                        string from_id = "0";
                        string from_name = String2Json(row["分类"].ToString());
                        from_id = AppManage.GetFromDict(from_name);
                        /*if (from_name == "8001N4 湖南沐阳") { from_id = "1"; }
                        if (from_name == "8001y6 爱诺") { from_id = "2"; }
                        if (from_name == "8001D3 艾条") { from_id = "3"; }
                        if (from_name == "8001B2 雷允上销售分公司") { from_id = "4"; }
                        if (from_name == "8001E6 初贝") { from_id = "5"; }
                        if (from_name == "8001B5 丽珠集团") { from_id = "5"; }
                        if (from_name == "8001P3 安徽安科") { from_id = "6"; }
                        if (from_name == "8001D1 贵州德良方药业") { from_id = "7"; }*/



                        string sql = $" insert INTO `dsnxs`(" +
                            $"`ymd`," +
                            $" seller_code," +
                            $" seller_name," +
                            $" purchaser_code ," +
                            $" purchaser_name ," +
                            $" articode ," +
                            $" class_id ," +
                            $" prd_code ," +
                            $" prd_name ," +
                            $" prd_spec ," +
                            $" prd_batch ," +
                            $" prd_qty ," +
                            $" prd_unit ," +
                            $" prd_price ," +
                            $" total ," +
                            $" mfrs ," +
                            $" pzwh ," +
                            $" sell_type ," +
                            $" delivery_address, " +
                            $" from_id , " +
                            $" from_name , " +
                            $" made_time " +
                            $") " +
                            $"VALUES (" +
                            $"'{ymd}'," +
                            $" '{seller_code}'," +
                            $" '{seller_name}'," +
                            $"'{purchaser_code}'," +
                            $"'{purchaser_name}'," +
                            $"'{articode}'," +
                            $"'{class_id}'," +
                            $"'{prd_code}'," +
                            $"'{prd_name}'," +
                            $"'{prd_spec}'," +
                            $"'{prd_batch}'," +
                            $"'{prd_qty}'," +
                            $"'{prd_unit}'," +
                            $"'{prd_price}'," +
                            $"'{total}'," +
                            $"'{mfrs}'," +
                            $"'{pzwh}'," +
                            $"'{sell_type}'," +
                            $"'{delivery_address}'," +
                            $"'{from_id}'," +
                            $"'{from_name}'," +
                            $" now() " +
                            $") ";


                        sqlList.Add(sql);
                        //LogBLL.log("sql：" + sql);
                    }

                    //sqlList.Add("call test_log()");

                    //LogBLL.log("SQL执行开始");
                    //sqlList.Add("call editGoodsKey()");
                    bool isok = MyOpDB.SaveDataToDB(strCnn_to, sqlList);
                    LogBLL.log("SQL执行:" + isok);

                    sync_count = DT.Rows.Count;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
                else
                {
                    LogBLL.log("未找到数据");
                    sync_count = 0;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
            }
            catch (Exception ex)
            {
                sync_message = "同步数据失败，错误消息=" + ex.Message;
                sync_result = "error";
                LogBLL.Err(sync_message);
            }
            finally
            {
                try
                {
                    // 记录日志
                    MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_sync_log (sync_message,sync_result,sync_count,made_time) VALUES ('{sync_name} {sync_message}', '{sync_result}', '{sync_count}',now()) ");

                }
                catch (Exception e)
                {

                }
                // 统计当天销售数据
                // MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_zdxx_log (sync_message,sync_result,sync_count,made_time) VALUES ('今日销售数据数量：{sale_count}条', '{sync_result}', '{sale_count}',now()) ");

            }
        }

        /// <summary>
        /// 档案表-复合分类（同步一次）
        /// </summary>
        private void sync_DSNFXFHFL()
        {
            string sync_name = "[DSNFXFHFL ]";
            
            string sync_message = "";
            string sync_result = "fail";
            int sync_count = 0;
            string sale_count = "";

            string strCnn_from = AppConfigManager.LoadConfig().dbConn_from;
            string strCnn_to = AppConfigManager.LoadConfig().dbConn_to;

            try
            {
                LogBLL.log("---" + sync_name + "--- 开始");

                //查询主档数据
                string sql_from = $" select * from DSNFXFHFL where 1=1 ";
                DataSet ds = OracleHelper.Query(strCnn_from, sql_from);
                DataTable DT = ds.Tables[0];
                if (DT != null && DT.Rows.Count > 0)
                {
                    LogBLL.log("行数：" + DT.Rows.Count);

                    List<string> sqlList = new List<string>();
                    sqlList.Add(" delete from DSNFXFHFL ;");
                    foreach (DataRow row in DT.Rows)
                    {

                        /*string deptid = row["deptid"].ToString();
                        string deptno = row["deptno"].ToString();
                        string deptname = row["deptname"].ToString();*/

                        string class_id = String2Json(row["ID号"].ToString());
                        string class_name = String2Json(row["复合分类名称"].ToString());

                        string sql = $" replace INTO `DSNFXFHFL`(" +
                            $" class_id," +
                            $" class_name, " +
                            $" made_time " +
                            $") " +
                            $"VALUES (" +
                            $"'{class_id}'," +
                            $" '{class_name}'," +
                            $" now()" +
                            $") ";


                        sqlList.Add(sql);
                        //LogBLL.log("sql：" + sql);
                    }

                    bool isok = MyOpDB.SaveDataToDB(strCnn_to, sqlList);
                    LogBLL.log("SQL执行:"+ isok);

                    sync_count = DT.Rows.Count;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";

                }
                else
                {
                    LogBLL.log("未找到数据");
                    sync_count = 0;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
            }
            catch (Exception ex)
            {
                sync_message = "同步数据失败，错误消息=" + ex.Message;
                sync_result = "error";
                LogBLL.Err(sync_message);
            }
            finally
            {
                try
                {
                    // 记录日志
                    MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_sync_log (sync_message,sync_result,sync_count,made_time) VALUES ('{sync_name} {sync_message}', '{sync_result}', '{sync_count}',now()) ");

                }
                catch (Exception e)
                {

                }
                // 统计当天销售数据
                // MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_zdxx_log (sync_message,sync_result,sync_count,made_time) VALUES ('今日销售数据数量：{sale_count}条', '{sync_result}', '{sale_count}',now()) ");

            }
        }

        /// <summary>
        /// 档案表-收款类型（同步一次）
        /// </summary>
        private void sync_DSNSKLX()
        {
            string sync_name = "[DSNSKLX   ]";
            string sync_message = "";
            string sync_result = "fail";
            int sync_count = 0;
            string sale_count = "";

            string strCnn_from = AppConfigManager.LoadConfig().dbConn_from;
            string strCnn_to = AppConfigManager.LoadConfig().dbConn_to;

            try
            {
                LogBLL.log("---" + sync_name + "--- 开始");

                //查询主档数据
                string sql_from = $" select * from DSNSKLX where 1=1 ";

                DataSet ds = OracleHelper.Query(strCnn_from, sql_from);
                DataTable DT = ds.Tables[0];
                if (DT != null && DT.Rows.Count > 0)
                {
                    LogBLL.log("行数：" + DT.Rows.Count);

                    List<string> sqlList = new List<string>();
                    foreach (DataRow row in DT.Rows)
                    {

                        /*string deptid = row["deptid"].ToString();
                        string deptno = row["deptno"].ToString();
                        string deptname = row["deptname"].ToString();*/

                        string pay_type_id = String2Json(row["收款类型ID"].ToString());
                        string pay_type_name = String2Json(row["收款类型名称"].ToString());

                        string sql = $" replace INTO `DSNSKLX`(" +
                            $" pay_type_id," +
                            $" pay_type_name " +
                            $") " +
                            $"VALUES (" +
                            $"'{pay_type_id}'," +
                            $" '{pay_type_name}'" +
                            $") ";


                        sqlList.Add(sql);
                        //LogBLL.log("sql：" + sql);
                    }

                    bool isok = MyOpDB.SaveDataToDB(strCnn_to, sqlList);
                    LogBLL.log("SQL执行:" + isok);



                    sync_count = DT.Rows.Count;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
                else
                {
                    LogBLL.log("未找到数据");
                    sync_count = 0;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
            }
            catch (Exception ex)
            {
                sync_message = "同步数据失败，错误消息=" + ex.Message;
                sync_result = "error";
                LogBLL.Err(sync_message);
            }
            finally
            {
                try
                {
                    // 记录日志
                    MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_sync_log (sync_message,sync_result,sync_count,made_time) VALUES ('{sync_name} {sync_message}', '{sync_result}', '{sync_count}',now()) ");

                }
                catch (Exception e)
                {

                }
                // 统计当天销售数据
                // MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_zdxx_log (sync_message,sync_result,sync_count,made_time) VALUES ('今日销售数据数量：{sale_count}条', '{sync_result}', '{sale_count}',now()) ");

            }
        }

        /// <summary>
        /// 数据分析
        /// </summary>
        private void sync_DSNFXCS(string new_from_name,string asyn_time)
        {
            if(new_from_name != "")
            {
                return;
            }

            string sync_name = "[DSNFXCS   ]";
            string sync_message = "";
            string sync_result = "fail";
            int sync_count = 0;
            string sale_count = "";

            string strCnn_from = AppConfigManager.LoadConfig().dbConn_from;
            string strCnn_to = AppConfigManager.LoadConfig().dbConn_to;

            try
            {
                LogBLL.log("---"+ sync_name + "--- 开始");

                //查询主档数据
                
                string sql_from = "select * from DSNFXCS where  to_char( 销售日期,'yyyy-mm-dd') ='" + asyn_time + "'   ";
                //string sql_from = $" select * from DSNFXCS where to_date('销售日期','yyyy-mm-dd') = to_date('" + asyn_time + "','yyyy-mm-dd') ";
                //string sql_from = $" select * from DSNFXCS where 1=1 order by 销售日期 desc ";
                //sql_from = "select * from (" + sql_from + ") where rownum <= 100"; // 取100条数据
                //LogBLL.log("sql_from1：" + sql_from);

                DataSet ds = OracleHelper.Query(strCnn_from, sql_from);
                DataTable DT = ds.Tables[0];
                if (DT != null && DT.Rows.Count > 0)
                {
                    LogBLL.log("行数：" + DT.Rows.Count);

                    List<string> sqlList = new List<string>();


                    foreach (DataRow row in DT.Rows)
                    {

                        /*string deptid = row["deptid"].ToString();
                        string deptno = row["deptno"].ToString();
                        string deptname = row["deptname"].ToString();*/

                        string order_id = String2Json(row["销售单号"].ToString());
                        string ymd = String2Json(row["销售日期"].ToString());
                        string dept_code = String2Json(row["部门ID"].ToString());
                        string dept_name = String2Json(row["部门名称"].ToString());
                        string class_id = String2Json(row["品种ID"].ToString());
                        string prd_name = String2Json(row["品名"].ToString());
                        string prd_spec = String2Json(row["规格"].ToString());
                        string prd_unit = String2Json(row["单位"].ToString());
                        string mfrs = String2Json(row["生产厂商"].ToString());

                        string tax_rate = String2Json(row["税率"].ToString());
                        string sell_qty = String2Json(row["数量"].ToString());
                        string sell_price = String2Json(row["含税金额"].ToString());
                        //string sell_amt = String2Json(row["销售金额"].ToString());

                        string pay_type = String2Json(row["支付类型"].ToString());
                        string age = String2Json(row["年龄"].ToString());
                        string sex = String2Json(row["性别"].ToString());
                        string shop_attr = String2Json(row["店铺属性"].ToString());


                        string sql = $" insert INTO `DSNFXCS`(" +
                            $"`order_id`," +
                            $" ymd ," +
                            $" dept_code ," +
                            $" dept_name ," +
                            $" class_id ," +
                            $" prd_name ," +
                            $" prd_spec ," +
                            $" prd_unit ," +
                            $" mfrs ," +
                            $" tax_rate ," +
                            $" sell_qty ," +
                            $" sell_price  ," +
                            $" pay_type ," +
                            $" age ," +
                            $" sex ," +
                            $" shop_attr ," +
                            $" made_time " +
                            $") " +
                            $"VALUES (" +
                            $"'{order_id}'," +
                            $"'{ymd}'," +
                            $"'{dept_code}'," +
                            $"'{dept_name}'," +
                            $"'{class_id}'," +
                            $"'{prd_name}'," +
                            $"'{prd_spec}'," +
                            $"'{prd_unit}'," +
                            $"'{mfrs}'," +
                            $"'{tax_rate}'," +
                            $"'{sell_qty}'," +
                            $"'{sell_price}'," +
                            $"'{pay_type}'," +
                            $"'{age}' ," +
                            $"'{sex}'," +
                            $"'{shop_attr}'," +
                            $"now()" +
                            $") ";


                        sqlList.Add(sql);
                        //LogBLL.log("sql：" + sql);
                    }

                    //sqlList.Add("call test_log()");

                    //LogBLL.log("SQL执行开始");

                    bool isok = MyOpDB.SaveDataToDB(strCnn_to, sqlList);


                    LogBLL.log("SQL执行结束:" + isok);



                    sync_count = DT.Rows.Count;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
                else
                {
                    LogBLL.log("未找到数据");
                    sync_count = 0;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
            }
            catch (Exception ex)
            {
                sync_message = "同步数据失败，错误消息=" + ex.Message;
                sync_result = "error";
                LogBLL.Err(sync_message);
            }
            finally
            {
                try
                {
                    // 记录日志
                    MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_sync_log (sync_message,sync_result,sync_count,made_time) VALUES ('{sync_name} {sync_message}', '{sync_result}', '{sync_count}',now()) ");

                }
                catch (Exception e)
                {

                }

                // 统计当天销售数据
                // MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_zdxx_log (sync_message,sync_result,sync_count,made_time) VALUES ('今日销售数据数量：{sale_count}条', '{sync_result}', '{sale_count}',now()) ");

            }
        }


        /// <summary>
        /// 档案表-复合分类（每天同步一次）
        /// </summary>
        private void sync_DSNSUPPLY()
        {
            string sync_name = "[DSNSUPPLY ]";

            string sync_message = "";
            string sync_result = "fail";
            int sync_count = 0;
            string sale_count = "";

            string strCnn_from = AppConfigManager.LoadConfig().dbConn_from;
            string strCnn_to = AppConfigManager.LoadConfig().dbConn_to;

            try
            {
                LogBLL.log("---" + sync_name + "--- 开始");

                //查询主档数据
                // // select UncArtiClassCode, artiid from Tbc UncArtiClassRela where UncArtiClassCode like"8001%';
                string sql_from = $" select * from DSNSUPPLY where 1=1 ";
                DataSet ds = OracleHelper.Query(strCnn_from, sql_from);
                DataTable DT = ds.Tables[0];
                if (DT != null && DT.Rows.Count > 0)
                {
                    LogBLL.log("行数：" + DT.Rows.Count);

                    List<string> sqlList = new List<string>();
                    sqlList.Add(" delete from DSNSUPPLY ;");
                    foreach (DataRow row in DT.Rows)
                    {

                        string supply_type = String2Json(row["供应商类别"].ToString());
                        string artiid = String2Json(row["商品id"].ToString());

                        string sql = $" replace INTO `DSNSUPPLY`(" +
                            $" supply_type," +
                            $" artiid, " +
                            $" made_time " +
                            $") " +
                            $"VALUES (" +
                            $"'{supply_type}'," +
                            $" '{artiid}'," +
                            $" now()" +
                            $") ";


                        sqlList.Add(sql);
                    }

                    bool isok = MyOpDB.SaveDataToDB(strCnn_to, sqlList);
                    LogBLL.log("SQL执行:" + isok);

                    sync_count = DT.Rows.Count;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";

                }
                else
                {
                    LogBLL.log("未找到数据");
                    sync_count = 0;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
            }
            catch (Exception ex)
            {
                sync_message = "同步数据失败，错误消息=" + ex.Message;
                sync_result = "error";
                LogBLL.Err(sync_message);
            }
            finally
            {
                try
                {
                    // 记录日志
                    MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_sync_log (sync_message,sync_result,sync_count,made_time) VALUES ('{sync_name} {sync_message}', '{sync_result}', '{sync_count}',now()) ");

                }
                catch (Exception e)
                {

                }
            }
        }

        /// <summary>
        /// 档案表-复合分类（每天同步一次）
        /// </summary>
        private void sync_DSNCLFL()
        {
            string sync_name = "[DSNCLFL ]";

            string sync_message = "";
            string sync_result = "fail";
            int sync_count = 0;
            string sale_count = "";

            string strCnn_from = AppConfigManager.LoadConfig().dbConn_from;
            string strCnn_to = AppConfigManager.LoadConfig().dbConn_to;

            try
            {
                LogBLL.log("---" + sync_name + "--- 开始");

                //查询主档数据
                // // select UncArtiClassCode, artiid from Tbc UncArtiClassRela where UncArtiClassCode like"8001%';
                string sql_from = $" select * from DSNCLFL where 1=1 ";
                DataSet ds = OracleHelper.Query(strCnn_from, sql_from);
                DataTable DT = ds.Tables[0];
                if (DT != null && DT.Rows.Count > 0)
                {
                    LogBLL.log("行数：" + DT.Rows.Count);

                    List<string> sqlList = new List<string>();
                    sqlList.Add(" delete from DSNCLFL ;");
                    foreach (DataRow row in DT.Rows)
                    {

                        string clfl = String2Json(row["陈列分类"].ToString());
                        string artiid = String2Json(row["商品id"].ToString());

                        string sql = $" replace INTO `DSNCLFL`(" +
                            $" clfl," +
                            $" artiid, " +
                            $" made_time " +
                            $") " +
                            $"VALUES (" +
                            $"'{clfl}'," +
                            $" '{artiid}'," +
                            $" now()" +
                            $") ";


                        sqlList.Add(sql);
                    }

                    bool isok = MyOpDB.SaveDataToDB(strCnn_to, sqlList);
                    LogBLL.log("SQL执行:" + isok);

                    sync_count = DT.Rows.Count;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";

                }
                else
                {
                    LogBLL.log("未找到数据");
                    sync_count = 0;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
            }
            catch (Exception ex)
            {
                sync_message = "同步数据失败，错误消息=" + ex.Message;
                sync_result = "error";
                LogBLL.Err(sync_message);
            }
            finally
            {
                try
                {
                    // 记录日志
                    MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_sync_log (sync_message,sync_result,sync_count,made_time) VALUES ('{sync_name} {sync_message}', '{sync_result}', '{sync_count}',now()) ");

                }
                catch (Exception e)
                {

                }
            }
        }


        /// <summary>
        /// 商品档案表（每天同步一次）
        /// </summary>
        private void sync_TBV_ARTICLE()
        {
            string sync_name = "[TBV_ARTICLE ]";

            string sync_message = "";
            string sync_result = "fail";
            int sync_count = 0;
            string sale_count = "";

            string strCnn_from = AppConfigManager.LoadConfig().dbConn_from;
            string strCnn_to = AppConfigManager.LoadConfig().dbConn_to;

            try
            {
                LogBLL.log("---" + sync_name + "--- 开始");

                //查询主档数据
                // // select UncArtiClassCode, artiid from Tbc UncArtiClassRela where UncArtiClassCode like"8001%';
                string sql_from = $" select artiid,articode,artiname,artispec,artiunit from TBV_ARTICLE where 1=1 order by artiid  ";
                DataSet ds = OracleHelper.Query(strCnn_from, sql_from);
                DataTable DT = ds.Tables[0];
                if (DT != null && DT.Rows.Count > 0)
                {
                    LogBLL.log("行数：" + DT.Rows.Count);

                    int batchSize = 100; // 每批执行的 SQL 数量
                    List<string> sqlList = new List<string>();
                    sqlList.Add(" delete from TBV_ARTICLE ;");
                    foreach (DataRow row in DT.Rows)
                    {

                        
                        string artiid        = String2Json(row["artiid"].ToString());         // 商品ID
                        //string articlasscode = String2Json(row["articlasscode"].ToString());  // 
                        string articode      = String2Json(row["articode"].ToString());       // 商品编码
                        //string artispell     = String2Json(row["artispell"].ToString());      // 拼音
                        string artiname      = String2Json(row["artiname"].ToString());       // 商品名
                        //string artiabbr      = String2Json(row["artiabbr"].ToString());       // 
                        string artispec      = String2Json(row["artispec"].ToString());       // 规格
                        string artiunit      = String2Json(row["artiunit"].ToString());       // 单位
                        //string artibrand     = String2Json(row["artibrand"].ToString());      // 
                        //string modelcode     = String2Json(row["modelcode"].ToString());      // 生产厂家

                        string sql = $" replace INTO `TBV_ARTICLE`(" +
                            
                            $" artiid," +
                            $" articode," +
                            $" artiname," +
                            $" artispec," +
                            $" artiunit," +
                            $" made_time " +
                            $") " +
                            $"VALUES (" +
                            $"'{artiid}'," +
                            $" '{articode}'," +
                            $" '{artiname}'," +
                            $" '{artispec}'," +
                            $" '{artiunit}'," +
                            $" now()" +
                            $") ";


                        sqlList.Add(sql);
                        //LogBLL.log("" + sql);

                        // 每当 sqlList 的数量达到 batchSize 时，执行一次批量插入
                        if (sqlList.Count >= batchSize)
                        {
                            bool isok0 = MyOpDB.SaveDataToDB(strCnn_to, sqlList);
                            if (!isok0)
                            {
                                // 如果执行失败，可以在这里处理错误
                                LogBLL.log("执行 SQL 时发生错误" + isok0);
                                break;
                            }
                            LogBLL.log("执行完成：" + batchSize);
                            sqlList.Clear(); // 清空 sqlList，为下一批准备
                        }
                    }

                    // 处理剩余的 SQL 语句
                    if (sqlList.Count > 0)
                    {
                        bool isok = MyOpDB.SaveDataToDB(strCnn_to, sqlList);
                        LogBLL.log("SQL执行:" + isok);

                    }


                    sync_count = DT.Rows.Count;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";

                }
                else
                {
                    LogBLL.log("未找到数据");
                    sync_count = 0;
                    sync_message = "同步数据成功，数量：" + sync_count;
                    sync_result = "success";
                }
            }
            catch (Exception ex)
            {
                sync_message = "同步数据失败，错误消息=" + ex.Message;
                sync_result = "error";
                LogBLL.Err(sync_message);
            }
            finally
            {
                try
                {
                    // 记录日志
                    MydbmanagerSql.ExecuteNonQuerySql(strCnn_to, $"insert into deyi_sync_log (sync_message,sync_result,sync_count,made_time) VALUES ('{sync_name} {sync_message}', '{sync_result}', '{sync_count}',now()) ");

                }
                catch (Exception e)
                {

                }
            }
        }


        /// <summary>
        /// 过滤特殊字符
        /// </summary>
        /// <param name="s">字符串</param>
        /// <returns>json字符串</returns>
        private static string String2Json(String s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s.ToCharArray()[i];
                switch (c)
                {
                    case '\'':
                        sb.Append("\\\'"); break;
                    case '\"':
                        sb.Append("\\\""); break;
                    case '\\':
                        sb.Append("\\\\"); break;
                    case '/':
                        sb.Append("\\/"); break;
                    case '\b':
                        sb.Append("\\b"); break;
                    case '\f':
                        sb.Append("\\f"); break;
                    case '\n':
                        sb.Append("\\n"); break;
                    case '\r':
                        sb.Append("\\r"); break;
                    case '\t':
                        sb.Append("\\t"); break;
                    default:
                        sb.Append(c); break;
                }
            }
            return sb.ToString();
        }

    }
}
